<include file='Index/header' />
<style type="text/css">
    .plupload_container {
        max-height:200px;
        max-width: 450px;
        position: relative;
    }
</style>
<div class="row">
	<br>
	<form id="homepage_form" action='{:U("Admin/Homepage/updateHomePagePicture")}' method="post">
		<div class="col-sm-12">
			<div class="col-sm-2" style="padding-top:5px">
				<label for="b_repository" class="control-label">选择仓库</label>
			</div>
			<div class="col-sm-6">
				<ul id="repository_tree" class="ztree"></ul>
			</div>
  	</div>

  	<div class="col-sm-12">
	  	<br>
	    <div class="col-sm-2" style="padding-left:15px;padding-top:5px">
				<label for="position" class="control-label">轮播图位置：</label>
			</div>
			<div class="col-sm-3">
				<select class="form-control" id="position" name="position">
					<option value="1">位置1</option>
					<option value="2">位置2</option>
					<option value="3">位置3</option>
				</select>
			</div>
		</div>
	
		<div class="col-sm-12">
  		<br>
	    <div class="col-sm-2" style="padding-left:15px;padding-top:5px">
				<label class="control-label">轮播图片：</label>
			</div>
			<div class="col-sm-6">
				<div id="uploadImg">
			    <p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
				</div>
			</div>
			<div class="col-sm-3">
				<label class="control-label">&nbsp;&nbsp;已上传图片：</label>
			</div>
			<div id="img" class="col-sm-3">
				
			</div>
		</div>

		<div class="col-sm-12">
  		<br>
	    <div class="col-sm-2" style="padding-left:15px;padding-top:5px">
				<label for="linktype" class="control-label">链接方式：</label>
			</div>
			<div class="col-sm-3">
				<select class="form-control" id="linktype" name="linktype">
					<option value="1">单一商品</option>
					<option value="2">宣传图片</option>
					<option value="3">商品分类</option>
				</select>
			</div>
		</div>

		<div id="linktype1" class="col-sm-12">
  		<br>
	    <div class="col-sm-2" style="padding-left:15px;padding-top:5px">
				<label for="barcode" class="control-label">商品编号：</label>
			</div>
			<div class="col-sm-3">
				<input id="product_id" name="product_id" class="form-control"></input>
			</div>
		</div>

		<div id="linktype2" class="col-sm-12" style="display:none">
  		<br>
	    <div class="col-sm-2" style="padding-left:15px;padding-top:5px">
				<label class="control-label">宣传图片：</label>
			</div>
			<div class="col-sm-6">
				<div id="uploadXCImg">
			    <p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
				</div>
			</div>
			<div class="col-sm-3">
				<label class="control-label">&nbsp;&nbsp;已上传图片：</label>
			</div>
			<div id="xcimg" class="col-sm-4">

			</div>
		</div>

		<div id="linktype3" style="display:none">
			<div class="col-sm-12">
				<br>
				<div class="col-sm-2" style="padding-top:5px">
					<label for="class1" class="control-label">商品类别：</label>
				</div>
				<div class="col-sm-3">
		      <ul id="product_class_tree" class="ztree"></ul>
		    </div>	
	  	</div>
  	</div>
	</form>

		<div class="col-sm-12">
			<br><br>
			<div class="col-sm-2"></div>
			<div class="col-sm-2">
    		<button class="btn btn-primary" onclick="submit()">保存</button>
    	</div>
    	<div class="col-sm-3">
    		<button class="btn btn-primary">取消</button>
    	</div>
    	<br><br>
	  </div>

</div>

<include file='Index/footer' />
<script>
$(document).ready(function() {
	loadRepositoryTree('repository_tree', true, false);
	loadProductClassTree('product_class_tree', false, false);
	loadPlupload();
	init();
 });

function searchBRepository(){
}
function searchARepository(a_repository_id){
	loadHomepagePicture(a_repository_id);
}

function loadHomepagePicture(a_repository_id){
	//所有控件状态初始化
	$('#img').html('');
	$("#linktype").find("option[value='1']").attr("selected",true); 
	changeSelect();
	$('#product_id').attr('value', '');
	$('#xcimg').html('');
	var treeObj = $.fn.zTree.getZTreeObj("product_class_tree");
	treeObj.cancelSelectedNode();
	
	if(!a_repository_id){
		//获取参数，开始进行ajax请求
		var treeObj = $.fn.zTree.getZTreeObj("repository_tree");
		var nodes = treeObj.getSelectedNodes();
		console.log(nodes[0]);
		if(nodes.length == 0 || typeof(nodes[0].a_repository_id) == 'undefined'){
			alert("仓库还没有初始化好，请重新选择！");
			return;
		}
		a_repository_id = nodes[0].a_repository_id;
	}
	
	var position = $('#position').val();
	$.ajax({
			url: '{:U("Admin/Homepage/getHomepagePictrue/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"a_repository_id":a_repository_id,
				"position":position
			}
		})
		.success(function(data){
			if(data){
				imgurl = data.imgurl;
				$('#img').html('<img width="300px" src="'+data.imgurl+'">');
				$("#linktype").find("option[value='"+data.linktype+"']").attr("selected",true); 
				changeSelect();
				if(data.linktype == '1'){
					$('#product_id').attr('value', data.content);
				}else if(data.linktype == '2'){
					xc_imgurl = data.content;
					$('#xcimg').html('<img width="300px" src="'+data.content+'">');
				}else if(data.linktype == '3'){
					var treeObj = $.fn.zTree.getZTreeObj("product_class_tree");
					var node = treeObj.getNodeByParam("product_class_id", data.content);
					treeObj.selectNode(node);
				}
			}
	});
}

function init(){
	$('#position').change(function(e){
		loadHomepagePicture();
	});

	$('#linktype').change(function(){
		changeSelect();
	});
}

function changeSelect(){
	for (var i = 1; i <= 3; i++) {
		if($("#linktype").val() != i){
			$('#linktype' + $("#linktype").val()).attr("style", "");
			$('#linktype' + i).attr("style", "display:none;");
		}
	}
}

var imgurl = '', xc_imgurl = '';
function loadPlupload(){
	$("#uploadImg").plupload({
    // General settings
    runtimes : 'html5,flash,silverlight,html4',
    url : '{:U("Admin/Homepage/upload")}',

    // User can upload no more then 20 files in one go (sets multiple_queues to false)
    max_file_count:1,
    chunk_size: '10mb',
    filters : {
        // Maximum file size
        max_file_size : '10mb',
        // Specify what files to browse for
        mime_types: [
            {title : "Image files", extensions : "jpg,gif,png"}
            /* {title : "Zip files", extensions : "zip"}*/
        ]
    },

    // Rename files by clicking on their titles
    rename: false,
    // Sort files
    sortable: true,
    // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
    dragdrop: true,
    //unique_names:true,
    // Views to activate
    views: {
        list: true,
        thumbs: true, // Show thumbs
        active: 'thumbs'
    },
    // Flash settings
    flash_swf_url : '__PUBLIC__/bower_components/plupload/js/Moxie.swf',

    // Silverlight settings
    silverlight_xap_url : '__PUBLIC__/bower_components/plupload/js/Moxie.xap',

    //确定上传事件
    init: {
        /*FileUploaded: function (up, file, result) {
            alert(result.response);
        }*/
        BeforeUpload:function(uploader,file){
					if(imgurl){
						alert("将会覆盖之前上传的轮播图片！");
					}
        },
        FileUploaded:function(uploader,file,responseObject){
        	var obj = eval('('+responseObject.response+')'); 
        	imgurl = rootUrl+'/Public/upload/homepage/' + obj.savename;
        	$('#img').html('<img width="300px" src="'+imgurl+'">');
					//console.log(responseObject);
        },
        UploadComplete:function(up,file){
            //console.log(up);
            //console.log(file);
        }
    }
	});
	
	$("#uploadXCImg").plupload({
    // General settings
    runtimes : 'html5,flash,silverlight,html4',
    url : '{:U("Admin/Homepage/upload")}',

    // User can upload no more then 20 files in one go (sets multiple_queues to false)
    max_file_count:1,
    chunk_size: '10mb',
    filters : {
        // Maximum file size
        max_file_size : '10mb',
        // Specify what files to browse for
        mime_types: [
            {title : "Image files", extensions : "jpg,gif,png"}
            /* {title : "Zip files", extensions : "zip"}*/
        ]
    },

    // Rename files by clicking on their titles
    rename: false,
    // Sort files
    sortable: true,
    // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
    dragdrop: true,
    //unique_names:true,
    // Views to activate
    views: {
        list: true,
        thumbs: true, // Show thumbs
        active: 'thumbs'
    },
    // Flash settings
    flash_swf_url : '__PUBLIC__/bower_components/plupload/js/Moxie.swf',

    // Silverlight settings
    silverlight_xap_url : '__PUBLIC__/bower_components/plupload/js/Moxie.xap',

    //确定上传事件
    init: {
        /*FileUploaded: function (up, file, result) {
            alert(result.response);
        }*/
        BeforeUpload:function(uploader,file){
					if(xc_imgurl){
						alert("将会覆盖之前上传的宣传图片！");
					}
        },
        FileUploaded:function(uploader,file,responseObject){
        	var obj = eval('('+responseObject.response+')'); 
        	xc_imgurl = rootUrl+'/Public/upload/homepage/' + obj.savename;
        	$('#xcimg').html('<img width="300px" src="'+xc_imgurl+'">');
					//console.log(responseObject);
        },
        UploadComplete:function(up,file){
            //console.log(up);
            //console.log(file);
        }
    }
	});
}

function submit(){
	var treeObj = $.fn.zTree.getZTreeObj("repository_tree");
	var nodes = treeObj.getSelectedNodes();
	if(nodes.length == 0 || typeof(nodes[0].a_repository_id) == 'undefined'){
		alert("请选择一个A仓库！");
		//window.location.href = '#repository_tree';
		return;
	}
	var a_repository_id = nodes[0].a_repository_id;

	if(imgurl == ''){
		alert("需要上传一张轮播图片！");
		window.location.href = '#uploadImg';
		return;
	}

	var content = '';
	if($('#linktype').val() == '1'){
		content = $('#product_id').val();
	}else if($('#linktype').val() == '2'){
		if(xc_imgurl == ''){
			alert("需要上传一张宣传图片！");
			window.location.href = '#uploadXCImg';
			return;
		}	
		content = xc_imgurl;
	}else if($('#linktype').val() == '3'){
		treeObj = $.fn.zTree.getZTreeObj("product_class_tree");
		nodes = treeObj.getSelectedNodes();
		
		if(nodes.length == 0){
			alert("请选择一个商品分类！");
			return;
		}
		content = nodes[0].product_class_id;
	}
	
	$('#homepage_form').ajaxSubmit({
  		data: {
        'imgurl': imgurl,
        'a_repository_id':a_repository_id,
        'content':content
      },
      success:function(data){
        location.reload();
      }
  });
}


</script>
</script>